define(['app'], function(app) {
    app.controller('MockModuleController', function($scope, $compile, $http, xDialog, toastr) {
        var self = this;
        self.modules = [];

        self.init = function() {
            $http.get('/mock/module/list').then(function(response) {
                if (response.status == 200) {
                    self.modules = response.data;
                }
            });
        }

        self.edit = function(module) {
            module = module || '';
            xDialog.open().editModule(module).then(function(success) {
                if (success) {
                    setTimeout(function() {
                        self.init();
                    }, 500);
                }
            });
        }

        self.proxy = function(module) {
            xDialog.open().editMockProxy({ module: module }).then(function(success) {
                if (success) {
                    setTimeout(function() {
                        self.init();
                    }, 500);
                }
            });
        }

        self.remove = function(module) {
            xDialog.confirm('确认信息', '您确定要删除【' + module + '】模块吗？').then(function(yes) {
                if (yes) {
                    $http.delete('/mock/module/delete?module=' + module).then(function(response) {
                        if (response.data == 'required') {
                            xDialog.alert('提示信息', '数据模块【' + module + '】不存在！');
                        } else if (response.data) {
                            toastr.success('数据模块【' + module + '】已删除！');
                            setTimeout(function() {
                                self.init();
                            }, 500);
                        }
                    });
                }
            });
        }

        self.callback = function(height, width, refresh) {
            // Toolbar
            if (refresh) {
                $('.navbar-form').append($compile($('#opt-toolbar').html())($scope));
            }
        }
    });
});
